package cn.wilson.hrm.service.impl;

import cn.wilson.hrm.domain.Employee;
import cn.wilson.hrm.domain.Tenant;
import cn.wilson.hrm.dto.EnteringDTO;
import cn.wilson.hrm.mapper.EmployeeMapper;
import cn.wilson.hrm.mapper.MealMapper;
import cn.wilson.hrm.mapper.TenantMapper;
import cn.wilson.hrm.service.ITenantService;
import cn.wilson.hrm.utils.encrypt.MD5;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wilson.liu
 * @since 2020-04-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private MealMapper mealMapper;

    @Override
    public void entering(EnteringDTO enteringDTO) {
        Date date = new Date();
        //保存租户
        Tenant tenant = enteringDTO.getTenant();
        //默认入驻时间为当前时间
        tenant.setRegisterTime(date);
        baseMapper.insert(tenant);

        //保存员工
        Employee employee = enteringDTO.getEmployee();
        employee.setUsername(employee.getUsername());
        employee.setTel(employee.getTel());
        employee.setEmail(employee.getEmail());
        //密码加密
        employee.setPassword(MD5.getMD5(employee.getPassword() + "lqs"));
        //设置默认添加时间
        employee.setInputTime(date);
        //设置默认状态为正常
        employee.setState(Employee.STATIC_ZC);
        //设置employee中的机构外键
        employee.setTenantId(tenant.getId());
        //设置默认的员工类型  管理员
        employee.setType(Employee.TYPE_GL);
        employeeMapper.insert(employee);

        //保存中间表
        //保存机构和套餐的中间表，所以可以直接调用套餐的mapper  要保存套餐id、机构id和一个状态
        mealMapper.saveMealAndTenant(enteringDTO.getMealId(),tenant.getId(),0);
        System.out.println(enteringDTO);
    }
}
